<?php

declare(strict_types=1);

namespace App\Controller\Backend;

use App\Controller\AbstractController;
use App\Model\Mobile;
use App\Request\Backend\MobileCreateRequest;
use App\Request\Backend\MobileEditRequest;
use App\Request\Backend\MobileIdRequest;
use App\Resource\BaseCollection;
use App\Resource\BaseResource;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;

class MobilesManage extends AbstractController
{
    public function index(RequestInterface $request, ResponseInterface $response)
    {
        $where = [];

        if ($request->input('mobile_area')) {

            $where[] = ['mobile_area', '=', $request->input('mobile_area')];
        }

        if ($request->input('mobile')) {

            $where[] = ['mobile', 'like', "{$request->input('mobile')}"];
        }

        if ($request->input('card_no')) {

            $where[] = ['card_no', 'like', "{$request->input('card_no')}"];
        }

        $list = Mobile::where($where)->orderByDesc('updated_at')->paginate();

        return new BaseCollection($list);
    }

    public function mobileAdd(MobileCreateRequest $request, ResponseInterface $response)
    {
        $data = $request->validated();
        Mobile::updateOrCreate(
            $data,$data
        );
        return new BaseResource();
    }

    public function mobileEdit(MobileEditRequest $request, ResponseInterface $response)
    {
        $data = $request->validated();
        Mobile::where('id', $data['id'])->update($data);
        return new BaseResource();
    }

    public function mobileDel($id, ResponseInterface $response)
    {
        Mobile::where('id', $id)->delete();
        return new BaseResource();
    }
}
